package util

import "strings"

// CamelCase2Underline 驼峰命名转下划线命名
func CamelCase2Underline(in string) string {
	bu := strings.Builder{}
	for i := 0; i < len(in); i++ {
		t := in[i]
		if t >= 'A' && t <= 'Z' {
			bu.WriteByte('_')
			t = in[i] ^ ' ' // 大小写转换
		}
		bu.WriteByte(t)
	}
	return bu.String()
}

// Underline2BigCamelCase 下划线命名转大驼峰命名
func Underline2BigCamelCase(in string) string {
	if in == "" {
		return ""
	}
	r := Underline2CamelCase(in)
	bu := strings.Builder{}
	bu.WriteByte(r[0] ^ ' ')
	bu.WriteString(r[1:])
	return bu.String()
}

// Underline2CamelCase 下划线命名转小驼峰命名
func Underline2CamelCase(in string) string {
	if in == "" {
		return ""
	}
	bu := strings.Builder{}
	for i := 0; i < len(in); i++ {
		t := in[i]
		if t == '_' {
			if i++; i < len(in) {
				bu.WriteByte(in[i] ^ ' ')
			}
		} else {
			bu.WriteByte(t)
		}
	}
	return bu.String()
}

// ReverseString 翻转字符串
func ReverseString(in string) string {
	ru := []rune(in)
	for left, right := 0, len(ru)-1; left < right; left, right = left+1, right-1 {
		ru[left], ru[right] = ru[right], ru[left]
	}
	return string(ru)
}

// GenerateString 生成字符串，s 表示原始字符串， count 表示重复次数
func GenerateString(s string, count int) string {
	bu := strings.Builder{}
	for i := 0; i < count; i++ {
		bu.WriteString(s)
	}
	return bu.String()
}
